﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.ComponentModel;
using Microsoft.SharePoint.WebPartPages;
using VPLink2013.Framework;

namespace VPLink2013.Webparts
{
    public class ViePratique : WebpartVPLink
    {
        #region get HTML
        /// <summary>
        /// Returns the HTML code rendered by this web part
        /// </summary>
        /// <returns>HTL code as string</returns>
        protected override string GetHtml()
        {
            string strHtml = string.Empty;
            ContextTrace.PrefixFileName = "webpart ViePratique";

            string strPage = this.Context.Request.Url.AbsolutePath;
            ContextTrace.AddTrace("strPage : " + strPage);

            string strList = this.Title;
            if (string.IsNullOrEmpty(strList) || strList == "ViePratique")
            {
                return " ";
            }

            string strHtmlFileCache = pathCache + @"\ViePratique\" + strList + ".htm";

            try
            {
                FileSerialization.CheckFileCacheExists(strHtmlFileCache, 5);

                if (System.IO.File.Exists(strHtmlFileCache))
                {
                    strHtml = System.IO.File.ReadAllText(strHtmlFileCache);
                    ContextTrace.AddTrace("File cache found !");
                }
                else
                {
                    if (!string.IsNullOrEmpty(ParamSharepointSite))
                    {
                        Uri uriSite = new Uri(ParamSharepointSite);
                        string strUrl = new Uri(uriSite, "/ViePratique").ToString();
                        ContextTrace.AddTrace("strUrl : " + strUrl);

                        using (SPSite site = new SPSite(strUrl))
                        {
                            ContextTrace.AddTrace("site : " + site.Url);
                            using (SPWeb web = site.OpenWeb())
                            {
                                ContextTrace.AddTrace("strList : " + strList);
                                SPList lst = web.Lists.TryGetList(strList);
                                if (lst != null)
                                {
                                    ProcessViePratique(lst);
                                    strXslFileName = "ViePratique.xsl";

                                    string strXml = FileSerialization.CreateXmlStringFromObj(global);

                                    string strXslPath = pathVP + @"\XSL\ViePratique\" + strXslFileName;
                                    ContextTrace.AddTrace("GetHtml.strXslPath = " + strXslPath);
                                    strHtml = GetTransform(strXml, strXslPath);
                                }
                                TraceFile(strHtmlFileCache, strHtml);
                            }
                        }
                    }
                }
            }
            catch (System.Exception exc)
            {
                ContextTrace.AddTrace("Erreur GetHtml : " + exc.Message + "/" + exc.StackTrace);
            }

            return strHtml;
        }
        #endregion get HTML

        private void ProcessViePratique(SPList lst)
        {
            global = new GlobalVPLink2013();
            global.ListeAnnonces = new List<Annonce>();
            global.Context = new Context();
            global.Context.Message = lst.Description;
            global.Context.Action = this.Title;

            SPListItemCollection items = lst.Items;

            foreach (SPListItem item in items)
            {
                Annonce ann = new Annonce();
                ann.Titre = VPLink2013SPHelper.GetValue(item, "Titre");
                ann.Body = VPLink2013SPHelper.GetValue(item, "Contenu");
                ann.Ordre = VPLink2013SPHelper.GetValue(item, "Ordre");
                global.ListeAnnonces.Add(ann);
            }
        }
    }
}
